//
//  locationTransformer.m
//  Ants
//
//  Created by ZHM on 16/7/18.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "locationTransformer.h"
#import <CoreLocation/CoreLocation.h>

@implementation locationTransformer

/**     允许转换    */
+ (BOOL)allowsReverseTransformation {
    return YES;
}
/**     转换成什么类    */
+ (Class)transformedValueClass {
    return [NSData class];
}
- (id)transformedValue:(id)value {
    CLLocation *location = (CLLocation *)value;
    NSNumber *latitude = [NSNumber numberWithDouble:location.coordinate.latitude];
    NSNumber *longitude = [NSNumber numberWithDouble:location.coordinate.longitude];
    NSDictionary *dic = @{@"latitude":latitude,@"longitude":longitude};
    NSData *dataFromLocation = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    return dataFromLocation;
}
- (id)reverseTransformedValue:(id)value {
    NSData *data = (NSData *)value;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    double latitude = [dictionary[@"latitude"] doubleValue];
    double longitude = [dictionary[@"longitude"] doubleValue];
    CLLocation *locationFromData = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    return locationFromData;
}
@end

